import * as Cesium from "cesium";
import BaseDraw from "./BaseDraw";
/**
 * 派生类-绘制点
 */
export default class DrawPoint extends BaseDraw {
  constructor(options) {
    super(options);
    this.currentDrawId = ""; // 当次绘制的-id
    this.labelEntity = null; // 标签实例
    this.pointEntity = null; // 点实例
    this.positionList = []; // 当次绘制的笛卡尔数组
  }
  resetDraw() {
    this.currentDrawId = this.generateUUID(); // 当次绘制的-id
    this.labelEntity = null; // 当次绘制的-圆实例
    this.pointEntity = null; // 当次绘制的-圆实例
    this.positionList = []; // 当次绘制的笛卡尔数组
  }
  addEntityCollection(entity) {
    this.entityCollection.add(entity, this.currentDrawId);
  }
  getAllEntity() {
    return this.entityCollection.getAll();
  }
  createPointEntity(cartesian) {
    const entity = new Cesium.Entity({
      position: cartesian, // 世界坐标
      point: {
        color: Cesium.Color.YELLOW,
        pixelSize: 15,
        disableDepthTestDistance: Number.POSITIVE_INFINITY,
      },
    });

    return entity;
  }
  createLabelEntity(cartesian) {
    const posList = this._coorder.cartesian3ToLonLatHeight(cartesian, 4); // 世界坐标转化为经纬度坐标
    const entity = new Cesium.Entity({
      position: cartesian, // 世界坐标
      label: {
        text: `经度：${posList[0]},纬度：${posList[1]}`,
        font: "20px",
        color: Cesium.Color.RED,
        pixelOffset: new Cesium.Cartesian2(0, -20),
        fillColor: Cesium.Color.WHITE,
        showBackground: true,
        backgroundColor: Cesium.Color.BLUE,
      },
    });

    return entity;
  }
}
/**
 * 开始绘制点
 * @param {* 是否显示测量Label} showMeasureLabel
 * @param {* 是否连续绘制} continuousDraw
 */
DrawPoint.prototype.startDraw = function (
  showMeasureLabel = true,
  continuousDraw = true
) {
  const _this = this;
  this._tip.show("单击开始绘制");
  this.resetDraw(); // 重置绘制中间量
  const customType = this.customType; // 自定义事件类型
  // 通过事件管理器新增事件 -左键单击事件
  this._handler.addHandelEvent(
    (event) => {
      const cartesian = this._coorder.ScreenToCartesian3(event.position); // 屏幕坐标转世界坐标
      if (!Cesium.defined(cartesian)) return;
      if (showMeasureLabel) {
        // 标注实体
        _this.labelEntity = _this.createLabelEntity(cartesian);
        _this.addEntityCollection(_this.labelEntity); // 加入集合
      }
      // 点实体
      _this.pointEntity = _this.createPointEntity(cartesian);
      _this.addEntityCollection(_this.pointEntity); // 加入集合

      if (!continuousDraw) {
        // 不连续绘制
        _this.endDraw();
      }
    },
    customType,
    Cesium.ScreenSpaceEventType.LEFT_CLICK
  );
  // 通过事件管理器新增事件 -鼠标移动事件
  this._handler.addHandelEvent(
    (event) => {
      const { x, y } = event.endPosition;
      this._tip.moveTipDOMPosition(x, y); // 改变 TipDOM 位置
    },
    customType,
    Cesium.ScreenSpaceEventType.MOUSE_MOVE
  );
  // 通过事件管理器新增事件 -右键单击事件
  this._handler.addHandelEvent(
    () => {
      _this.endDraw();
    },
    customType,
    Cesium.ScreenSpaceEventType.RIGHT_CLICK
  );
};

/**
 * 结束绘制点
 */
DrawPoint.prototype.endDraw = function () {
  this._tip.close();
  // 移除绑定的事件
  const customType = this.customType;
  this._handler.removeHandelEvnet(
    customType,
    Cesium.ScreenSpaceEventType.LEFT_CLICK
  );
  this._handler.removeHandelEvnet(
    customType,
    Cesium.ScreenSpaceEventType.MOUSE_MOVE
  );
  this._handler.removeHandelEvnet(
    customType,
    Cesium.ScreenSpaceEventType.RIGHT_CLICK
  );
  this.resetDraw(); // 重置绘制中间量
};
/**
 * 清除绘制的点
 */
DrawPoint.prototype.removeDraw = function () {
  this.entityCollection.removeAll(); // 清空集合所有
};
